package org.balabala.user.service.impl;

import com.alibaba.fastjson.TypeReference;
import org.balabala.user.entity.CommentEntity;
import org.balabala.user.entity.VideoEntity;
import org.balabala.user.feign.VideoFeign;
import org.balabala.user.utils.Query;
import org.balabala.user.dao.FollowInfoDao;
import org.balabala.user.entity.FollowInfoEntity;
import org.balabala.user.service.FollowInfoService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("followInfoService")
public class FollowInfoServiceImpl extends ServiceImpl<FollowInfoDao, FollowInfoEntity> implements FollowInfoService {

    @Autowired
    private VideoFeign videoFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FollowInfoEntity> page = this.page(
                new Query<FollowInfoEntity>().getPage(params),
                new QueryWrapper<FollowInfoEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByUid(Map<String, Object> map) {
        return null;
    }


//    @Override
//    public PageUtils queryPageByUid(Map<String, Object> map) {
//        IPage<FollowInfoEntity> page = this.page(
//                new Query<FollowInfoEntity>().getPage(map),
//                new QueryWrapper<FollowInfoEntity>().eq("uid",map.get("uid"))
//        );
//
//        List<FollowInfoEntity> records = page.getRecords();
//        List<Long> collect = records.stream().map(item -> item.getFanId()).collect(Collectors.toList());
//        //调用用户服务，进行视频信息的查询
//        Resp resp = videoFeign.listByUid(collect);
//        List<VideoEntity> data = resp.getData(new TypeReference<List<VideoEntity>>() {
//        });
//        //
//        if(resp.getCode() == 0){
//            for(int i=0;i<records.size();i++) {
//                records.get(i).setVideoEntity(data.get(i));
//            }
//        }else{
//            return null;
//        }
//        return new PageUtils(page);
//    }

    @Override
    public Resp saveInfo(Long fanId, Long followedId) {

        FollowInfoEntity follow = this.baseMapper.selectOne(new QueryWrapper<FollowInfoEntity>().eq("fan_id", fanId).eq("followed_id", followedId));
        if(follow != null){
            follow.setFollowTime(new Date());
            if(follow.getIsCancel() == 1){
                follow.setIsCancel(0);
                int i = this.baseMapper.updateById(follow);
                if(i == 1){
                    return Resp.ok("取关成功");
                }else {
                    return Resp.error(500,"取关失败");
                }
            }else {
                follow.setIsCancel(1);
                int i = this.baseMapper.updateById(follow);
                if(i == 1){
                    return Resp.ok("关注成功");
                }else {
                    return Resp.error(500,"关注失败");
                }
            }
        }else{
            FollowInfoEntity followInfoEntity = new FollowInfoEntity();
            followInfoEntity.setFanId(fanId);
            followInfoEntity.setFollowedId(followedId);
            followInfoEntity.setFollowTime(new Date());
            followInfoEntity.setIsCancel(1);
            int insert = this.baseMapper.insert(followInfoEntity);
            if(insert == 1){
                return  Resp.ok("关注成功");
            }else{
                return Resp.error(500,"关注失败");
            }
        }


    }

    @Override
    public PageUtils queryPageByFanId(HashMap<String, Object> hashMap) {
        IPage<FollowInfoEntity> page = this.page(
                new Query<FollowInfoEntity>().getPage(hashMap),
                new QueryWrapper<FollowInfoEntity>().eq("fan_id",hashMap.get("fanId"))
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByFollowedId(HashMap<String, Object> hashMap) {
        IPage<FollowInfoEntity> page = this.page(
                new Query<FollowInfoEntity>().getPage(hashMap),
                new QueryWrapper<FollowInfoEntity>().eq("followed_id",hashMap.get("followedId"))
        );
        return new PageUtils(page);
    }


    @Override
    public Resp delete(Long fanId, Long followId) {
        this.baseMapper.delete(new QueryWrapper<FollowInfoEntity>().eq("fan_id",fanId).eq("followed_id",followId));
        return Resp.ok();
    }

}